package com.example.a517;

import android.os.Bundle;
import android.util.Log;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONObject;

import java.util.List;

public class GSONTest extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_gsontest);
        String json = "{\n" +
                "    \"name\": \"John\",\n" +
                "    \"age\": 30,\n" +
                "    \"city\": \"New York\"\n" +
                "}";
        String jsonArray = "[\n" +
                "            {\n" +
                "                name:\"杰克\",age:18,id:5,gender:\"男\"\n" +
                "            },\n" +
                "            {\n" +
                "                name:\"李雷\",age:20,id:2,gender:\"男\"\n" +
                "            },\n" +
                "            {\n" +
                "                name:\"韩梅梅\",age:16,id:9,gender:\"女\"\n" +
                "            },\n" +
                "            {\n" +
                "                name:\"汤姆\",age:16,id:2,gender:\"男\"\n" +
                "            },\n" +
                "        ]\n";
        Gson gson = new Gson();
        Person person = gson.fromJson(json, Person.class);

        Log.e("TAG", "onCreate: " +person );

        Gson gsonArray = new Gson();
        List<People> people = gsonArray.fromJson(jsonArray, new TypeToken<List<People>>() {
        }.getType());
        for (People person1 : people) {
            System.out.println(person1);
        }

    }
}
class People{
    public String name;
    public int age;
    public int id;
    public String gender;

    @Override
    public String toString() {
        return "People{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", id=" + id +
                ", gender='" + gender + '\'' +
                '}';
    }
}
class Person {
    public String name;
    public int age;
    public String city;

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", city='" + city + '\'' +
                '}';
    }
}